<?php
/**
 * 流转订单列表(供机构查看本机构的所有流转订单)
 */
?>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button id="btnBatchApprove" class="btn btn-primary" disabled type="button" data-toggle="modal"><i
                        class="fa fa-edit"></i> 批量审核订单
                </button>
            </div>
        </div>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">

            <div class="form-group col-lg-3">
                <div class="input-group input-large custom-date-range" data-date="13/07/2013"
                     data-date-format="mm/dd/yyyy">
                    <input name="from" id="start_time" class="form-control dpd1" type="text" placeholder="开始日期"
                           value="<?= $start_time ?>">
                    <span class="input-group-addon">To</span>
                    <input name="to" id="end_time" class="form-control dpd2" type="text" placeholder="结束日期"
                           value="<?= $end_time ?>">
                </div>
            </div>

            <div class="form-group col-lg-2">
                <select class="form-control" id="searchFlowOrderStatus" style="width: 100%">
                    <option value="0" <?= $flow_order_status == '0' ? 'selected' : '' ?>>----全部状态----</option>
                    <option value="10" <?= $flow_order_status == '10' ? 'selected' : '' ?>>待审核订单</option>
                    <option value="12" <?= $flow_order_status == '12' ? 'selected' : '' ?>>未结算订单</option>
                    <option value="13" <?= $flow_order_status == '13' ? 'selected' : '' ?>>支付成功</option>
                    <option value="21" <?= $flow_order_status == '21' ? 'selected' : '' ?>>待确认订单</option>
                    <option value="31" <?= $flow_order_status == '31' ? 'selected' : '' ?>>出库中</option>
                    <option value="32" <?= $flow_order_status == '32' ? 'selected' : '' ?>>配送中</option>
                    <option value="40" <?= $flow_order_status == '40' ? 'selected' : '' ?>>已完成</option>
                    <option value="41" <?= $flow_order_status == '41' ? 'selected' : '' ?>>已取消</option>
                </select>

            </div>

            <div class="form-group col-lg-3">
                <div class="input-group m-bot15">
                    <div class="input-group-btn">
                        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">
                            <span id="search_field_name" data-field="<?= $field ?>">搜索字段</span>
                            <span class="caret"></span></button>
                        <ul class="dropdown-menu">
                            <li><a href="javascript:setOrderField('','搜索字段')">搜索字段</a></li>
                            <li><a href="javascript:setOrderField('code','订单号码')">订单号码</a></li>
                            <li><a href="javascript:setOrderField('user_name','下单用户')">下单用户</a></li>
                            <li><a href="javascript:setOrderField('goods_name','商品名称')">商品名称</a></li>
                            <li><a href="javascript:setOrderField('goods_code','商品代码')">商品代码</a></li>
                        </ul>
                    </div>
                    <input class="form-control" type="text" id="field_value" value="<?= $field_value ?>">
                </div>
            </div>

            <div class="form-group col-lg-3">
                <?= app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'searchInstitution', 'currentInstitutionCode' => $current_institution_code, 'defaultText' => '----选择查询机构----']) ?>
            </div>
            <button id="btnSearch" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th style="width: 40px;">
                        <div class="checkbox square-blue">
                            <input type="checkbox" id="chkAll">
                        </div>
                    </th>
                    <th>订单编号</th>
                    <th>下单用户</th>
                    <th>订单销售总金额</th>
                    <th>订单结算总金额</th>
                    <th>订单状态</th>
                    <th>下单时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    ?>
                    <tr>
                        <td>
                            <?php
                            if ($item['status'] == 10) {
                                ?>
                                <div class="checkbox square-blue">
                                    <input type="checkbox" name="flowOrder" data-flow-id="<?= $item['id'] ?>"
                                           data-code="<?= $item['order_code'] ?>">
                                </div>
                                <?php
                            }
                            ?>
                        </td>
                        <td><?= $item['order_code'] ?></td>
                        <td><?= $item['user_name'] ?></td>
                        <td>￥<?= $item['total_selling_price'] ?></td>
                        <td>￥<?= $item['total_settlement_price'] ?></td>
                        <td><?= $item['status_name'] ?></td>
                        <td><?= $item['create_datetime'] ?></td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-order-id="<?= $item['id'] ?>"
                                   href="#orderDetailModal">流转订单详情</a>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="orderDetailModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">订单流转信息</h4>
            </div>
            <div class="modal-body" id="order_detail_content">
                正在加载数据....
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
    $(function () {
        var isChkAllClick = false;
        $('#chkAll').on('ifClicked', function (event) {
            isChkAllClick = true;
        });

        $('#chkAll').on('ifChecked ifUnchecked', function (event) {
            if (isChkAllClick == false) {
                return;
            }
            if (event.type == 'ifChecked') {
                $('input:checkbox[name=flowOrder]').iCheck('check');
            } else {
                $('input:checkbox[name=flowOrder]').iCheck('uncheck');
            }

            isChkAllClick = false;
        });

        var isItemClick = false;
        $('input:checkbox[name=flowOrder]').on('ifClicked', function (event) {
            isItemClick = true;
        });
        $('input:checkbox[name=flowOrder]').on('ifChecked ifUnchecked', function (event) {
            if ($('input:checkbox[name=flowOrder]:checked').length > 0) {
                $('#btnBatchApprove').removeAttr('disabled');
            } else {
                $('#btnBatchApprove').attr('disabled', 'true');
            }

            if (isItemClick == false) {
                return;
            }
            if ($('input:checkbox[name=flowOrder]:checked').length == $('input:checkbox[name=flowOrder]').length) {
                $('#chkAll').iCheck('check');
            } else {
                $('#chkAll').iCheck('uncheck');
            }
            isItemClick = false;
        });

        $('#btnBatchApprove').click(function () {
            var r = confirm('批量审核这些订单?');
            if (r == false)return;

            var flowIdList = [];
            $("input:checkbox[name=flowOrder]:checked").each(function () {
                flowIdList.push($(this).data('flow-id'));
            });

            requestJson('batch-approve-order-flow-handler', {'order_flow_id_list': flowIdList}, true, function (responseData) {
                window.location.reload();
            });
        });

        $('#orderDetailModal').on('show.bs.modal', function (e) {
            $("#order_detail_content").html('正在加载数据....');
            var orderFlowId = e.relatedTarget.attributes['data-order-id'].value;
            loadOrderFlowDetailPartial(orderFlowId);
        });

        //初始化搜索字段
        var field = '<?=$field?>';
        switch (field) {
            case "":
                $("#search_field_name").html("搜索字段");
                break;
            case "code":
                $("#search_field_name").html("订单号码");
                break;
            case "user_name":
                $("#search_field_name").html("下单用户");
                break;
            case "goods_name":
                $("#search_field_name").html("商品名称");
                break;
            case "goods_code":
                $("#search_field_name").html("商品代码");
                break;
        }


        $('#btnSearch').click(function () {
            var url = window.location.pathname;

            var searchFlowOrderStatus = $('#searchFlowOrderStatus').val();
            if (searchFlowOrderStatus == 0) {
                url = removeUrlParam(url, 'flow_order_status');
            } else {
                url = setUrlParam(url, 'flow_order_status', searchFlowOrderStatus);
            }

            var searchInstitutionCode = $('#searchInstitution').val();
            if (searchInstitutionCode == 0) {
                alert("请选择查询机构!");
                return;
            } else {
                url = setUrlParam(url, 'institution_code', searchInstitutionCode);
            }

            var start_time = $("#start_time").val();
            if (start_time != null && start_time != '') {
                url = setUrlParam(url, 'start_time', start_time);
            }
            var end_time = $("#end_time").val();
            if (end_time != null && end_time != '') {
                url = setUrlParam(url, 'end_time', end_time);
            }

            var field = $("#search_field_name").attr("data-field");
            if (field != "" && field != null) {
                url = setUrlParam(url, 'field', field);
            }
            var field_value = $("#field_value").val();
            if (field_value != '') {
                url = setUrlParam(url, 'field_value', field_value);
            }

            url = setUrlParam(url, 'page', 1);
            window.location.href = url;
        });
    })


    function loadOrderFlowDetailPartial(orderFlowId) {
        requestHtml('order-flow-detail-partial', {order_flow_id: orderFlowId}, true, function (responseHtml) {
            $("#order_detail_content").html(responseHtml);
        });
    }

    function setOrderField(field_name, show_name) {
        $("#search_field_name").attr("data-field", field_name);
        $("#search_field_name").html(show_name);
    }
</script>


